package com.exp.base.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;

public class PathUtils {
	/**
	 * 获得当前程序路径
	 */
	public static String getCwdDirPath(){
		String cwdPath = System.getProperty("user.dir");
		return cwdPath;
	}
	/**
	 * 推荐使用 FileUtils.getTempDirectory()
	 * @return
	 */
	@Deprecated
	public static File getTmpDir(){
		String folder=System.getProperty("java.io.tmpdir");
		return new File(folder);
	}
	/**
	 * 简单的获得文件的方法， 文件必须存在
	 * ""做参数，获得当前classpath路径
	 * <b>支持完整的classpath表达式</b>
	 * <b>不支持jar内文件</b>
	 * @throws FileNotFoundException 
	 */
	public static File getClasspathFile(String resourceLocation) throws FileNotFoundException  {
		Assert.notNull(resourceLocation, "resourceLocation 不能为空");
		if(!resourceLocation.startsWith("classpath:")){
			resourceLocation = "classpath:"+resourceLocation;
		}
		File file = ResourceUtils.getFile(resourceLocation);
		return file;
	}
	/**
	 * 超级获得资源的方法， 文件必须存在
	 * <b>支持完整的classpath表达式</b>
	 * <b>支持jar内文件</b>
	 * @throws IOException 
	 */
	public static Resource[] getClasspathResources(String resourceLocation) throws IOException{
		Assert.notNull(resourceLocation, "resourceLocation 不能为空");
		if(!resourceLocation.startsWith("classpath")){
			resourceLocation = "classpath*:"+resourceLocation;
		}
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		Resource[] metaInfResources = resourcePatternResolver.getResources(resourceLocation);
		return metaInfResources;
	}
	
	
	
	
	
}
